package cc.owoo.godpen.reflect;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * Map对象值设置
 * Created by nimensei
 * 2022-06-04 上午 01:01
 */
public record MapSet(String key, TypeTranslate translate) implements ValueSet {
    @Override
    public boolean set(Object object, Object value) throws IllegalAccessException, InvocationTargetException {
        try {
            Map<Object, Object> map = (Map<Object, Object>) object;
            map.put(key, translate.parse(value, null));
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean fill(Object object, Object value) throws IllegalAccessException, InvocationTargetException {
        try {
            Map<Object, Object> map = (Map<Object, Object>) object;
            Object original = map.get(key);
            map.put(key, translate.parse(value, original));
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
